extends Node


@export var mob_scene: PackedScene
var score

# Called when the node enters the scene tree for the first time.
func _ready():
	# 随机化随机数生成器的种子（或内部状态）。当前的实现使用基于时间的数字来重新设定种子。
	randomize()
	# new_game()
	pass # Replace with function body.


func game_over():
	$Music.stop()
	$DeathSound.play()
	$ScoreTimer.stop()
	$MobTimer.stop()
	
	$HUD.show_game_over()
	
func new_game():
	score = 0
	$Music.play()
	$Player.start($StartPosition.position)
	$StartTimer.start()
	get_tree().call_group("mods", "queue_free")
	# 计分板
	$HUD.update_score(score)
	$HUD.show_message("Get Ready")
	
# 游戏角色被碰撞
func _on_Player_hit():
	game_over()
	pass # Replace with function body.


func _on_MobTimer_timeout():
	var mob = mob_scene.instantiate()
	
	#生产怪物线
	var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
	mob_spawn_location.progress_ratio = randf() # 设置随机距离点
	
	# 设置怪物旋转弧度 180
	var direction = mob_spawn_location.rotation + PI / 2
	mob.position = mob_spawn_location.position
	
	# 设置怪物移动方向
	direction += randf_range(-PI/4, PI/4)
	mob.rotation = direction
	
	# 设置随机速度向量
	var velocity = Vector2(randf_range(150, 250), 0)
	# linear_velocity = 速度向量
	mob.linear_velocity = velocity.rotated(direction)
	
	# 添加怪物子节点
	add_child(mob)
	


func _on_ScoreTimer_timeout():
	score += 1
	$HUD.update_score(score)


func _on_StartTimer_timeout():
	$MobTimer.start()
	$ScoreTimer.start()


func _on_HUD_start_game():
	new_game()
